<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px"
    >
      <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="用户状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary" icon="el-icon-search" size="mini"
          v-hasPermi="['system:user:query']" @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:user:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList">
      <el-table-column label="用户编号" align="center" key="id" prop="id"/>
      <el-table-column label="用户名称" align="center" key="name" prop="name"
                       :show-overflow-tooltip="true"
      />
      <el-table-column label="用户昵称" align="center" key="nickname" prop="nickname"
                       :show-overflow-tooltip="true"
      />

      <el-table-column label="google二维码" align="center" prop="google_auth_qrcode">
        <template slot-scope="scope">
          <el-popover trigger="click">
            <a :href="scope.row.google_auth_qrcode" target="_blank" title="查看大图">
              <vue-qr :text="scope.row.google_auth_qrcode" alt="" style="width: 350px; height: 350px; cursor: pointer"
                      crossOrigin="anonymous"/>
            </a>
            <vue-qr :text="scope.row.google_auth_qrcode" alt=""
                    style="width: 40px; height: 40px; cursor: pointer" slot="reference"
                    crossOrigin="anonymous"/>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="ip白名单" align="center" key="white_ip" prop="white_ip"
                       :show-overflow-tooltip="true"
      />
      <el-table-column prop="status" label="状态" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" prop="order_num" width="100"/>
      <el-table-column label="创建时间" align="center" key="create_time" prop="create_time" width="160"/>
      <el-table-column label="修改时间" align="center" key="update_time" prop="update_time" width="160"/>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.id !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:platform:edit']"
          >修改
          </el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
                       v-hasPermi="['system:user:resetPwd', 'system:user:authRole']"
          >
                <span class="el-dropdown-link">
                  <i class="el-icon-d-arrow-right el-icon--right"></i>更多
                </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleResetPwd" icon="el-icon-key"
                                v-hasPermi="['system:user:resetPwd']"
              >重置密码
              </el-dropdown-item>
              <el-dropdown-item command="handleAuthRole" icon="el-icon-circle-check"
                                v-hasPermi="['system:user:authRole']">分配角色
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="用户名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入用户名称" maxlength="30"
                      :disabled="form.id != undefined"/>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="form.nickname" placeholder="请输入昵称" maxlength="30"/>
          </el-form-item>
          <el-form-item label="用户密码" prop="password" v-if="form.id == undefined">
            <el-input v-model="form.password" placeholder="请输入用户密码" maxlength="30"/>
          </el-form-item>
          <el-form-item label="ip白名单" prop="white_ip">
            <el-input type="textarea" v-model="form.white_ip" placeholder="请输入登录ip白名单,按逗号隔开"
                      maxlength="500"/>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="验证码" prop="google_code">
            <el-input v-model="form.google_code" placeholder="请输入验证码" maxlength="30"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改配置对话框 -->
    <el-dialog :title="resetTitle" :visible.sync="resetPwdOpen" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="新密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入新密码" maxlength="30"/>
          </el-form-item>
          <el-form-item label="验证码" prop="google_code">
            <el-input v-model="form.google_code" placeholder="请输入验证码" maxlength="30"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="resetPwd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addUser, getUser, listUser, resetUserPwd, updateUser} from '@/api/system/user'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'SystemUser',
  computed: {},
  dicts: ['sys_normal_disable'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: '',
      // 弹出层标题
      resetTitle: '',
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      resetPwdOpen: false,
      admin: false,
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        name: undefined,
        status: undefined,
        app_id: 0
      },
      // 表单校验
      rules: {
        name: [
          {required: true, message: '用户名称不能为空', trigger: 'blur'},
          {min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '用户密码不能为空', trigger: 'blur'},
          {min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur'}
        ],
        google_code: [
          {required: true, message: 'google验证码不能为空', trigger: 'blur'},
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listUser(this.queryParams).then(response => {
          this.userList = response.data
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.resetPwdOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nickname: undefined,
        name: undefined,
        password: undefined,
        white_ip: '',
        google_code: '',
        status: 1,
        app_id: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTimeRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case 'handleResetPwd':
          this.handleResetPwd(row)
          break;
        case "handleAuthRole":
          this.handleAuthRole(row);
          break;
        default:
          break
      }
    },
    /** 分配角色操作 */
    handleAuthRole: function (row) {
      const id = row.id;
      this.$router.push("/system/user-auth/role/" + id);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加用户'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getUser(row.id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改用户'
      })
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.reset()
      this.form.id = row.id
      this.resetPwdOpen = true
      this.resetTitle = '重置密码'
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    }
    ,
    /** 提交按钮 */
    resetPwd: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          resetUserPwd(this.form.id, this.form.password, this.form.google_code).then(response => {
            this.$modal.msgSuccess('重置密码成功')
            this.resetPwdOpen = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
